//
//  MainViewController.swift
//  Weibo
//
//  Created by 张远文 on 16/4/25.
//  Copyright © 2016年 张远文. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    //监听方法
    @objc private func composeDidClick() {
        let compose = ComposeViewController()
        let nav = UINavigationController(rootViewController: compose)
        presentViewController(nav, animated: true, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        //用KVC进行对控制器tabBar的赋值
        let mainTabBar = MainTabBar()
        setValue(mainTabBar, forKey: "tabBar")
        
        //添加子控制器
        addChildViewControllers()
        
        mainTabBar.composeBtn.addTarget(self, action: #selector(composeDidClick), forControlEvents: .TouchUpInside)
    }
    
    private func addChildViewControllers() {
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    
    //重载
    private func addChildViewController(childController: UIViewController,title: String,imageName: String) {
        
        let nav = BaseNavViewController(rootViewController: childController)
        
        childController.title = title
        
        //tabBar.tintColor = UIColor.orangeColor()
        
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        
        //        Home.tabBarItem.selectedImage = UIImage(named: "tabbar_home_highlighted")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //
        //        Home.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
        addChildViewController(nav)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
